<template>
  <BasicModal v-bind="$attrs" @register="register" showFooter :title="getTitle" width="80%" @ok="handleSubmit"
    @cancel="closeFn">
    <div> 基础数量:<Input v-model:value="baseNum" style="width: 60%;" /></div>

    <div style=" width: 100%; height: 625px;overflow: hidden">
      <VxeBasicTable ref="tableRef" v-bind="gridOptions"> </VxeBasicTable>
    </div>
  </BasicModal>

</template>

<script setup lang="ts">
import { ref, computed, unref, reactive } from 'vue';
import { Button, Input } from 'ant-design-vue';

import { BasicModal, useModalInner } from '@/components/Modal';
import {
  BasicTableProps,
  VxeBasicTable,
  VxeGridInstance,
  VxeGridListeners,
} from '@/components/VxeTable';

import { outColumns, searchFormSchema, storageColumns } from './role.data';
import { useMessage } from "@/hooks/web/useMessage";
import { storageListApi } from '@/api/sys/wmStorage';
const props = defineProps({
  type: { type: String, default: null },//入库还是出口 in out
});
const emit = defineEmits(['success', 'register']);
const { createMessage } = useMessage();
const tableRef = ref<VxeGridInstance>();
const baseNum = ref("")

const gridOptions = reactive<BasicTableProps>({
  id: 'VxeTable',
  // keepSource: true,
  editConfig: { trigger: 'click', mode: 'cell', showStatus: true },
  showOverflow: true,
  size: 'mini',
  height: 'auto',
  columns: storageColumns,
  toolbarConfig: {
    enabled: false,
  },
  checkboxConfig: {
    labelField: 'name',
    highlight: true
  },
  rowConfig: {
    labelField: 'tid',
    trigger: 'row',
  },
  formConfig: {
    enabled: true,
    items: searchFormSchema,
  },

  proxyConfig: {
    props: {
      result: 'data.list',
      list: 'data.list',
      total: 'data.totalResult',
      message: 'message',
    },
    ajax: {
      query: async ({ page, form }) => {
        return storageListApi({
          page: page.currentPage,
          pageSize: page.pageSize,
          // gt0SkuStocks:1,
          ...form,
        });
      },
    },
  },
});

const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {

  baseNum.value = "";

  tableRef.value.commitProxy('reload');
  tableRef.value.clearCheckboxRow();
  tableRef.value.clearCheckboxReserve();

});

const getTitle = ref('选择库存商品');

async function handleSubmit() {
  let selectRows = tableRef.value.getCheckboxRecords(true);

  if (!selectRows) {
    createMessage.error('请先勾选数据!');
    return false;
  }
  selectRows.forEach(row => {
    if (baseNum.value) {
      if (props.type && props.type == "in") {
        row.inNum = baseNum.value
      }
      if (props.type && props.type == "out") {
        row.outNum = baseNum.value
      }
    }
  })
  emit('success', unref(selectRows));
  closeFn();
  // console.log("----selectIds",selectIds.value)
}
function closeFn() {
  // clearSelectedRowKeys();
  closeModal();
}

</script>

<style scoped></style>
